package com.weixin.gong.example.guava;

import com.google.common.base.Splitter;
import org.junit.Assert;
import org.junit.Test;

import java.util.List;

/**
 * @author weixin.gong
 * @date 17-5-15 下午2:13
 */
public class SplitterExample {

    @Test
    public void splitEmptyStringToList() {
        Splitter splitter = Splitter.on(",");
        List<String> list = splitter.splitToList("");
        System.out.println("size:" + list.size() + ",toString:" + list);
        Assert.assertArrayEquals(new String[]{""}, list.toArray());
    }

    @Test
    public void splitEmptyStringToListWithOmitEmptyStrings() {
        Splitter splitter = Splitter.on(",").omitEmptyStrings();
        List<String> list = splitter.splitToList("");
        System.out.println("size:" + list.size() + ",toString:" + list);
        Assert.assertArrayEquals(new String[]{}, list.toArray());
    }

    @Test(expected = NullPointerException.class)
    public void splitNullToList() {
        Splitter splitter = Splitter.on(",");
        splitter.splitToList(null);
    }

    @Test(expected = NullPointerException.class)
    public void splitNullToListWithOmitEmptyStrings() {
        Splitter splitter = Splitter.on(",");
        splitter.splitToList(null);
    }

    @Test
    public void splitToListWithTrimResults() {
        Splitter splitter = Splitter.on(",").trimResults();
        List<String> list = splitter.splitToList(" 1 , 2 ");
        System.out.println("size:" + list.size() + ",toString:" + list);
        Assert.assertArrayEquals(new String[]{"1", "2"}, list.toArray());
    }

    @Test
    public void splitSpaceToListWithOmitEmptyStrings() {
        Splitter splitter = Splitter.on(",").omitEmptyStrings();
        List<String> list = splitter.splitToList("  ");
        System.out.println("size:" + list.size() + ",toString:" + list);
        Assert.assertArrayEquals(new String[]{"  "}, list.toArray());
    }

    @Test
    public void splitSpaceToListWithTrimResults() {
        Splitter splitter = Splitter.on(",").trimResults();
        List<String> list = splitter.splitToList("  ");
        System.out.println("size:" + list.size() + ",toString:" + list);
        Assert.assertArrayEquals(new String[]{""}, list.toArray());
    }

    @Test
    public void splitSpaceToListWithOmitEmptyStringsAndTrimResults() {
        Splitter splitter = Splitter.on(",").omitEmptyStrings().trimResults();
        List<String> list = splitter.splitToList("  ");
        System.out.println("size:" + list.size() + ",toString:" + list);
        Assert.assertArrayEquals(new String[]{}, list.toArray());
    }
}
